<?php
session_start();
date_default_timezone_set('America/Los_Angeles');
include'connect_db.php';

if(!isset($_SESSION['user']))
	header("Location: login.php");

$user = mysql_real_escape_string($_SESSION['user']);
$result = mysql_query("SELECT * FROM user WHERE id = $user");
$row = mysql_fetch_array($result);

if($row['type'] != 2)
	die;

$id = mysql_real_escape_string($_POST['id']);

$returnArray = array();

$result = mysql_query("SELECT * FROM order_record WHERE user = $id");

while($row = mysql_fetch_array($result))
{
	$row['order_type'] = "Boosting";
	array_push($returnArray, $row);
}

$result = mysql_query("SELECT * FROM duo_order WHERE user = $id");

while($row = mysql_fetch_array($result))
{
	$row['order_type'] = "Coaching";
	array_push($returnArray, $row);
}

$result = mysql_query("SELECT * FROM leveling_order WHERE user = $id");

while($row = mysql_fetch_array($result))
{
	$row['order_type'] = "Leveling";
	array_push($returnArray, $row);
}

$result = mysql_query("SELECT * FROM referral_order WHERE user = $id");

while($row = mysql_fetch_array($result))
{
	$row['order_type'] = "Referral";
	array_push($returnArray, $row);
}


function aasort (&$array, $key) {
	$sorter=array();
	$ret=array();
	reset($array);
	foreach ($array as $ii => $va) {
		$sorter[$ii]=$va[$key];
	}
	asort($sorter);
	foreach ($sorter as $ii => $va) {
		$ret[$ii]=$array[$ii];
	}
	$array=$ret;
}

aasort($returnArray,"timestamp");

$x = 0;

while(isset($returnArray[$x]))
{
	echo "<tr><td>".$returnArray[$x]['id']."</td>";
	echo "<td>".date("d M Y g:i:sA", $returnArray[$x]['timestamp'])." PST</td>";
	echo "<td>".$returnArray[$x]['paypal']."</td>";
	echo "<td>".$returnArray[$x]['ip']."</td>";
	echo "<td>".$returnArray[$x]['summoner']."</td>";
	echo "<td>".$returnArray[$x]['order_type']."</td>";
	echo "<td>".$returnArray[$x]['payout']."</td>";
	if($returnArray[$x]['order_type'] == "Boosting")
		echo "<td><a class = 'btn btn-success btn-xs' href = 'order.php?id=".$returnArray[$x]['id']."'>Details</a></td></tr>";
	else if($returnArray[$x]['order_type'] == "Coaching")
		echo "<td><a class = 'btn btn-success btn-xs' href = 'duo_order.php?id=".$returnArray[$x]['id']."'>Details</a></td></tr>";
	else if($returnArray[$x]['order_type'] == "Leveling")
		echo "<td><a class = 'btn btn-success btn-xs' href = 'leveling_order.php?id=".$returnArray[$x]['id']."'>Details</a></td></tr>";
	else if($returnArray[$x]['order_type'] == "Referral")
		echo "<td><a class = 'btn btn-success btn-xs' href = 'referral_order.php?id=".$returnArray[$x]['id']."'>Details</a></td></tr>";
	$x++;
}
?>